package com.baox.agridata.synchronous.util;


import java.util.Base64;
import java.util.Random;
import java.util.UUID;

public class StringStrategy {


    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static String getUUIDNonHyphen() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 4位流水号
     * @return
     */
    public static String generateUniqueSerialNumber() {
        String serialNumber = UUID.randomUUID().toString();
        int hashCode = serialNumber.hashCode();
        int modulo = Math.abs(hashCode) % 10000;
        String fourDigitSerialNumber = String.format("%04d", modulo);
        return fourDigitSerialNumber;
    }


    public static String decode(String inStr) {
        byte[] in = Base64.getDecoder().decode(inStr.getBytes());
        byte[] bytes = xor(in, (byte) 4);
        String str = new String(bytes);
        return str;
    }

    public static String encode(String input) {
        byte[] in = xor(input.getBytes(), (byte) 4);
        byte[] bytes = Base64.getEncoder().encode(in);
        return new String(bytes);
    }


    public static byte[] xor(byte[] input, byte xor) {
        int len = input.length;
        byte[] target = new byte[len];
        for (int i = 0; i < len; i++) {
            target[i] = (byte) (input[i] ^ xor);
        }
        return target;
    }

    /**
     * 小写字母、数字、“-”构成，且不以“-”开头
     *
     * @param str
     * @return
     */
    public static boolean isValidString(String str) {
        //小写字母:97~122
        //数字：48~57
        //“-”:45
        if (str.charAt(0) == 45) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (((ch < 48) & (ch != 45)) || ((ch > 57) & (ch < 97)) || (ch > 122)) {
                return false;
            }
        }
        return true;
    }


    //密码应包括数字、小写字母、大写字母、特殊符号4类中至少3类；
    public static boolean checkPwd(String pwd) {
        String PW_PATTERN = "^(?![a-zA-Z]+$)(?![a-z\\d]+$)(?![a-z!@#\\$%]+$)(?![A-Z\\d]+$)(?![A-Z!@#\\$%]+$)(?![\\d!@#\\$%]+$)[a-zA-Z\\d!@#\\$%]+$";

        return pwd.matches(PW_PATTERN);
    }


    public static String getRandom(int len) {
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        for (int i = 0; i < len; i++) {
            rs.append(r.nextInt(10));
        }
        return rs.toString();
    }


    public static void main(String[] args) {
        System.out.println(getRandom(6));
    }


}
